package com.yami.shop.personnft.common.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.SceneType;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.personnft.common.dao.PersonNftAccountMapper;
import com.yami.shop.personnft.common.dao.PersonNftMapper;
import com.yami.shop.personnft.common.model.PersonNft;
import com.yami.shop.personnft.common.model.PersonNftAccount;
import com.yami.shop.personnft.common.service.PersonNftAccountService;
import com.yami.shop.service.ProductService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class PersonNftAccountServiceImpl implements PersonNftAccountService {
	@Autowired
	private ProductService productService;
	@Autowired
    private PersonNftAccountMapper personNftAccountMapper;
	@Autowired
    private PersonNftMapper personNftMapper;

	@Override
	public List<PersonNftAccount> listByParam(PersonNftAccount personNftAccount) {
		return personNftAccountMapper.listByParam(personNftAccount);
	}

	@Override
	public void save(PersonNftAccount personNftAccount) {
		personNftAccount.setCreateTime(DateUtil.date());
		personNftAccount.setId(IdUtil.getSnowflake().nextIdStr());
		personNftAccountMapper.save(personNftAccount);
	}
	@Override
	public IPage<PersonNftAccount> page(PageParam page, PersonNftAccount personNftAccount) {
		IPage<PersonNftAccount> doPage = personNftAccountMapper.page(page,personNftAccount);
		return doPage;
	}

	@Override
	public PersonNftAccount getDetail(PersonNftAccount personNftAccount) {
		List<PersonNftAccount> lsit = personNftAccountMapper.listByParam(personNftAccount);
		if(ObjectUtil.isEmpty(lsit)){
			return null;
		}
		PersonNftAccount personNft = lsit.get(0);
		return personNft;
	}

	@Override
	public void saveBatch(List<PersonNftAccount> list) {
		for (PersonNftAccount personNftAccount : list) {
			personNftAccount.setCreateTime(DateUtil.date());
			personNftAccount.setId(IdUtil.getSnowflake().nextIdStr());
		}
		personNftAccountMapper.saveBatch(list);
	}

	@Override
	public void updateBatchByUserIdAndTokenId(List<PersonNftAccount> list) {
		personNftAccountMapper.updateBatchByUserIdAndTokenId(list);
	}

	@Override
	public void updateBatchById(List<PersonNftAccount> list) {
		personNftAccountMapper.updateBatchById(list);
	}

	@Override
	public void consignBill(List<String> personIds) {
		List<PersonNft> personNftList = personNftMapper.getListByIds(personIds.toArray(new String[]{}));
		List<PersonNftAccount> list = new ArrayList<>();
		personNftList.stream().forEach(p -> {
			PersonNftAccount personNftAccount = PersonNftAccount.builder()
					.id(IdUtil.getSnowflake().nextIdStr())
					.sceneType(Integer.valueOf(SceneType.Consign.getCode()))
					.content(p.getTokenId())
					.dawProductCode(p.getDawProductCode())
					.denomId(p.getDenomId())
					.userId(p.getUserId())
					.createTime(DateUtil.date())
					.count(1)
					.productId(p.getProductId())
					.type(1)
					.build();
			list.add(personNftAccount);
		});
		personNftAccountMapper.saveBatch(list);
	}





}
